﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RATSharp.Validator;
using RATSharp.Model;
namespace RATSharp
{
    class MenuUtilities
    {
        public static void FirstAndLastDayOfMonth(DateTime date, out DateTime first, out DateTime last)
        {
            first = new DateTime(date.Year, date.Month, 1);
            DateTime nextFirst;
            if (first.Month == 12) nextFirst = new DateTime(first.Year + 1, 1, 1);
            else nextFirst = new DateTime(first.Year, first.Month + 1, 1);
            last = nextFirst.AddDays(-1);
        }

        public enum YES_NO_CHOICE { yes, no };

        public static T GetSimpleInput<T>(IInputValidator<T> validator)
        {
            while (true)
            {
                T parsedInput;
                Console.Write("> ");
                string decision = Console.ReadLine();
                if (validator.validate(decision, out parsedInput))
                {
                    return parsedInput;
                }
            }
        }

        /// <summary>
        /// This is a little bit hack but it will make data handling easier
        /// (and also it is working in the same way on server)
        /// </summary>
        /// <param name="reservations">reservations</param>
        public static void MakeBidirectionalConnection(Reservations reservations)
        {
            foreach (Reservation res in reservations)
            {
                foreach (Period period in res.periods)
                {
                    period.reservation = res;
                }
            }
        }


        public static bool WaitForCommTask<T>(Task<T> task, out T data)
        {
            data = default(T);
            try
            {
                task.Wait();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Some exception occured during communication:");
                Console.WriteLine(ex.InnerException);
                return false;
            }
            data = task.Result;
            return data != null;
        }
    }
}
